let sly = window['sly'] = window['sly'] ? window['sly'] : {}
function isNative() {
    return window['PlatformClass'] != undefined || window['native'] != undefined || window['jsb'] != undefined
}
if (isNative()) {
    const CLASS_NAME = 'sdk/JSBridge'

    const SHOW_BANNER = 'showBanner'
    const HIDE_BANNER = 'hideBanner'
    const LOAD_BANNER = 'loadBanner'

    const SHOW_INSERT = 'showInsertAd'
    const LOAD_INSERT = 'loadInsertAd'

    const LOAD_REWARD = 'loadRewardAd'
    const SHOW_REWARD = 'showRewardAd'


    var JsNativeBridge = {
        callbacks: {},
        isCocos() {
            return window['cc'] != undefined && (window['jsb'] != undefined || window['native'] != undefined)
        },
        isLaya() {
            return window['conchConfig'] != undefined && window['PlatformClass'] != undefined
        },
        callStaticMethod(className, methodName, parameters, methodSignOrFunc) {
            if (JsNativeBridge.isCocos()) {
                JsNativeBridge.cocosCallStaticMethod(className, methodName, parameters, methodSignOrFunc)
            } else {
                JsNativeBridge.layaCallStaticMethod(className, methodName, parameters, methodSignOrFunc)
            }
        },
        layaCallStaticMethod(className, methodName, parameters, param) {
            var os = window['conchConfig'].getOS();
            var callback = param.callback;
            var bridge;
            let PlatformClass = window['PlatformClass']
            if (os == "Conch-ios") {
                bridge = PlatformClass.createClass(className);//创建脚步代理
                if (!bridge) {
                    console.log(' callStatic Method class name ', className, methodName, parameters)
                    return;
                }
                if (callback) {
                    bridge.callWithBack(function (value) {
                        var obj = JSON.parse(value)
                        console.log(obj);
                        callback(obj)
                    }, methodName + ":", JSON.stringify(parameters));

                } else {
                    bridge.call(methodName, parameters)

                }

            }
            else if (os == "Conch-android") {
                //需要完整的类路径，注意与iOS的不同
                bridge = PlatformClass.createClass(className);//创建脚步代理
                if (!bridge) {
                    console.log(' callStatic Method class name ', className, methodName, parameters)
                    return;
                }
                if (callback) {
                    bridge.callWithBack(function (value) {
                        var obj = JSON.parse(value)
                        console.log(obj);
                        callback(obj)
                    }, methodName, JSON.stringify(parameters));
                } else {
                    bridge.call(methodName, parameters)
                }

            }
        },
        cocosCallStaticMethod(className, methodName, parameters, param) {
            let methodSignature = param.methodSignature;
            if (!methodSignature) {
                methodSignature = "(Ljava/lang/String;)V";
            }
            let callback = param.callback;
            if (callback) {
                let c = JsNativeBridge.callbacks[className]
                if (!c) {
                    c = {}
                    JsNativeBridge.callbacks[className] = c;
                }
                c[methodName] = callback;
            }
            let cc = window['cc']
            let jsb = window['jsb'] || window['native']
            console.log(" JsNativeBridge callStaticMethod ", cc.sys.isNative)
            if (!cc.sys.isNative) {
                return -1;
            }
            console.log(" JsNativeBridge callStaticMethod ", methodName, parameters)
            if (cc.sys.os == cc.sys.OS_IOS) {
                let result = jsb['reflection'].callStaticMethod(className, methodName, parameters);
                if (result) {
                    console.log("callStaticMethod ios  ", result);
                }
            } else {
                console.log(" JsNativeBridge callStaticMethod adroid ")
                let result = jsb['reflection'].callStaticMethod(className, methodName, methodSignature, parameters)
                console.log("callStaticMethod result  ", result);
                if (result) {

                    return result;
                }
            }
        },

        javaCallback(data) {
            if (data) {
                data = JSON.parse(data)
                if (data) {
                    let className = data.className;
                    let methodName = data.methodName;
                    let c = JsNativeBridge.callbacks[className]
                    if (c) {
                        let callback = c[methodName]
                        if (callback) {
                            callback(data.result)
                            c[methodName] = null;
                        }
                    }
                }

            }
        },
        bannerResize(data) {

        },
        rewardAdClose(data) {

        }
    }

    class NativeAd {
        constructor(data) {
            this.data = data
            this.loadListener = new sly.SdkEvent()
            this.errorListener = new sly.SdkEvent()
        }
        onLoad(func) {
            this.loadListener.on(func)
        }
        offLoad(func) {
            this.loadListener.off(func)
        }
        emitLoad(data) {
            this.loadListener.emit(data)
        }
        onError(func) {
            this.errorListener.on(func)

        }
        offError(func) {
            this.errorListener.off(func)
        }
        emitError(data) {
            this.errorListener.emit(data)
        }
        destroy() {
            this.loadListener.clear();
            this.errorListener.clear();
        }
    }


    //原生插屏广告
    class NativeInterstitialAd extends NativeAd {
        constructor(data) {
            super(data)
        }
        show() {
            JsNativeBridge.callStaticMethod(CLASS_NAME, SHOW_INSERT, '')
        }
        load() {
            JsNativeBridge.callStaticMethod(CLASS_NAME, LOAD_INSERT, JSON.stringify(this.data), { callback: this.loadCallback.bind(this) })
        }
        loadCallback(result) {
            if (result == 1) {
                this.insertIns.emitLoad(null)
            } else {
                this.insertIns.emitError('加载失败')
            }

        }
    }



    class NativeRewardedVideoAd extends NativeAd {
        constructor(data) {
            super(data)
            this.closeListener = new sly.SdkEvent()
        }
        load() {
            JsNativeBridge.callStaticMethod(CLASS_NAME, LOAD_REWARD, JSON.stringify(this.data), { callback: this.loadCallback.bind(this) })
        }
        show() {
            JsNativeBridge.callStaticMethod(CLASS_NAME, SHOW_REWARD, '')
        }
        loadCallback(result) {
            if (result == 1) {
                this.emitLoad(null)
            } else {
                this.emitError('加载失败')
            }
        }
        rewardAdClose(result) {
            this.emitClose({ isEnded: result })
        }
        destroy() {
            super.destroy();
            this.closeListener = new sly.SdkEvent()
        }
        onClose(func) {
            this.closeListener.on(func)
        }
        offClose(func) {
            this.closeListener.off(func)
        }
        emitClose(data) {
            this.closeListener.emit(data)
        }
    }

    class NativeBannerAd extends NativeAd {
        constructor(data) {
            super(data)
            this.resizeListener = new sly.SdkEvent()
        }
        load() {
            JsNativeBridge.callStaticMethod(CLASS_NAME, LOAD_BANNER,
                JSON.stringify(this.data), { callback: this.loadCallback.bind(this) })
        }
        hide() {
            JsNativeBridge.callStaticMethod(CLASS_NAME, HIDE_BANNER, '')
        }
        show() {
            JsNativeBridge.callStaticMethod(CLASS_NAME, SHOW_BANNER, '')
        }
        destroy() {
            super.destroy();
            this.resizeListener = new sly.SdkEvent()
        }
        //java call functions 
        loadCallback(result) {
            console.log('bannerAdCallback result ', result)
            if (result == 1) {
                this.emitLoad(null)
            } else {
                this.emitError('加载失败')
            }
        }
        bannerResize(size) {
            this.emitLoad(size)
        }
        onResize(func) {
            this.resizeListener.on(func)
        }
        offResize(func) {
            this.resizeListener.off(func)
        }
        emitResize(data) {
            this.resizeListener.emit(data)
        }
    }

    //原生适配
    class Native {
        constructor() {
            this.env = {
                USER_DATA_PATH: ""
            }
        }
        createBannerAd(data) {
            if (!this.bannerIns) {
                this.bannerIns = new NativeBannerAd(data)
            }
            return this.bannerIns
        }
        createRewardedVideoAd(data) {
            if (!this.rewardIns) {
                this.rewardIns = new NativeRewardedVideoAd(data)
            }
            return this.rewardIns
        }
        createInterstitialAd(data) {
            if (!this.insertIns) {
                this.insertIns = new NativeInterstitialAd(data)
            }
            return this.insertIns
        }

        getSystemInfoSync() {
            return sly.SystemInfo;
        }

        getDataPath() {
            return ''
        }

        getFileSystemManager() {
            return null;
        }


        getUpdateManager() {
            return null;
        }
    }
    window['JsNativeBridge'] = JsNativeBridge;
    sly.native = new Native();
}

